﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSW.Framework.Common.Binding;
using CSW.Framework.UI;

namespace LinqBusinessObjectBuilder.Forms
{
    internal partial class ToStringForm : Form
    {
        private readonly InplaceEditDataGridViewController<Test> m_Grid;
        private BindingList<Test> m_List;

        public ToStringForm()
        {
            InitializeComponent();

            m_Grid = new InplaceEditDataGridViewController<Test>(dataGridView1);
            m_Grid.AddField("Name", "Name");
            m_Grid.AddField("JobTitle", "JobTitle");
            m_Grid.AddField("HireDate", "HireDate");
            m_Grid.AddField("IsWhatever", "IsWhatever");
            m_Grid.Setup(true, true);

            m_List = new BindingList<Test>();
            m_List.Add(new Test { Name = "Hello" });
            m_List.Add(new Test { Name = "World", HireDate = DateTime.Now });

            m_Grid.DataSource = m_List;
        }

        private class Test : BusinessObject
        {
            private readonly Property<string> m_Name;
            private readonly Property<string> m_JobTitle;
            private readonly Property<DateTime?> m_HireDate;
            private readonly Property<bool> m_IsWhatever;

            public Test()
            {
                m_Name = new Property<string>(this, "Name");
                m_JobTitle = new Property<string>(this, "JobTitle");
                m_HireDate = new Property<DateTime?>(this, "HireDate");
                m_IsWhatever = new Property<bool>(this, "IsWhatever");

                BeginEdit(false);
            }

            public bool IsWhatever
            {
                get { return m_IsWhatever.Value; }
                set { m_IsWhatever.Value = value; }
            }

            public string Name
            {
                get { return m_Name.Value; }
                set { m_Name.Value = value; }
            }

            public string JobTitle
            {
                get { return m_JobTitle.Value; }
                set { m_JobTitle.Value = value; }
            }

            public DateTime? HireDate
            {
                get { return m_HireDate.Value; }
                set { m_HireDate.Value = value; }
            }

            public override bool TrySave(out string errorMessage)
            {
                if (!CheckAllRules(out errorMessage))
                    return false;

                IsEditting = false;

                //throw new NotImplementedException();
                return true;
            }

            public override bool IsNew
            {
                get { return true; }
            }

            public override void Refresh()
            {
                //throw new NotImplementedException();
            }
        }
    }
}
